package com.example.farmeri.ui.person_data;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.MainThread;

import com.example.farmeri.R;
import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.data.config.ServiceConfig;
import com.example.farmeri.http.API;
import com.example.farmeri.pojo.vo.BaseCallBack;
import com.example.farmeri.tools.OKHttpClientUtils;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.tools.TextFilterUtils;
import com.example.farmeri.ui.base.BaseActivity;
import com.example.farmeri.ui.login.LoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class UpdataNickNameActivity extends BaseActivity {

    @BindView(R.id.updata_nick_name)
    EditText text_nick;

    String id;
    String nickName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_nick_name);
        ButterKnife.bind(this);
        initToolBar(R.id.updata_introduce_toolbar, "修改昵称", true, true);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        nickName = intent.getStringExtra("nickName");
        text_nick.setText(nickName);
    }

    public void submit(View view) {
        if (id == null || id.equals("")){
            showToast(this,"用户Id不能为空");
            return;
        }
        if (!TextFilterUtils.filterIllegalText(text_nick.getText().toString(),1,15)){
            showToast("非法输入");
            reduceCredit(id);
            return;
        }
        OkHttpClient client = OKHttpClientUtils.getInstance();
        RequestBody requestBody = new FormBody.Builder()
                .add("id", id)
                .add("nickName",text_nick.getText().toString())
                .build();
        Request request = new Request.Builder()
                .url(ServiceConfig.updateNickName())
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new BaseCallBack(handler,0x11));
    }

    /**
     * 减少信誉值
     * @param id
     */
    private void reduceCredit(String id) {
        RequestBody requestBody = new FormBody.Builder()
                .add("id", id)
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .reduceCredit(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                        if(responseUtils.getCode() == 200){
                            DataParam.getUserInfo().setCredit(DataParam.getUserInfo().getCredit()-0.1f);
                            if (DataParam.getUserInfo().getCredit()<=0){
                                showToast("用户信誉值过低，已被锁定");
                                startActivity(new Intent(UpdataNickNameActivity.this, LoginActivity.class));
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showToast("更新信誉值失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        if (DataParam.getUserInfo().getCredit()<=0){
            showToast("用户信誉值过低，已被锁定");
            startActivity(new Intent(this, LoginActivity.class));
        }

    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x11){
                DataParam.getUserInfo().setNick_name(text_nick.getText().toString());
                finish();
            }
        }
    };
}